import moment from "moment";
//设置为中文
moment.locale('zh-cn')
export default {
  // 时间格式化  用法：
//   let date = new Date()
//   dateFormat("YYYY-mm-dd HH:MM", date)
// >>> 2019-06-06 19:45`
   dateFormat(fmt, date){
      let ret;
      const opt = {
        "Y+": date.getFullYear().toString(),        // 年
        "m+": (date.getMonth() + 1).toString(),     // 月
        "d+": date.getDate().toString(),            // 日
        "H+": date.getHours().toString(),           // 时
        "M+": date.getMinutes().toString(),         // 分
        "S+": date.getSeconds().toString()          // 秒
        // 有其他格式化字符需求可以继续添加，必须转化成字符串
      };
      for (let k in opt) {
        ret = new RegExp("(" + k + ")").exec(fmt);
        if (ret) {
          fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
        }
        ;
      }
      ;
      return fmt;
   },

  dateToFromNowDaily( myDate ) {
     let that = this;
      // get from-now for this date
      var fromNow = moment( myDate ).fromNow();

      // ensure the date is displayed with today and yesterday
      return moment( myDate ).calendar( null, {
        // when the date is closer, specify custom values
        lastWeek: '[上周]dd',
        lastDay:  '[昨天]',
        sameDay:  function(){
          let res = that.dateFormat('HH:MM', new Date(myDate));
          return'['+res+']';
        },
        nextDay:  '[明天]',
        nextWeek: '[下周]dd',
        // when the date is further away, use from-now functionality
        sameElse: function () {
          return "ll";
          // return "YYYY年MM月DD日";
        }
      });
    },

    UUID() {
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
      const r = (Math.random() * 16) | 0,
        v = c === 'x' ? r : (r & 0x3) | 0x8;
      return v.toString(16);
    });
  }
}
